const Actions = require('./actions');
import { monStorage, Storage } from 'clientConfig/util/StoreData';
import { NEW, RUNNING, TERMINATED, COMPLETED, CANCELED } from 'clientConfig/constants';
const DB = require('app/db.js');
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {

    },
    onGetData: function (pageNum, props, success, error,cb) {
        let t = this;
        // 
        DB.Notice.get_notice_lists({
            PageNum: pageNum,
            CountForAPage: props.pageSize,
            mes_receiver: Storage.get('user.id'),
            proj_name: props.proj_name ? props.proj_name : "",
            proj_num: props.proj_num ? props.proj_num : ""
        })
            .then((content) => {
                var WDCount = content.WDCount
                content = content.list;
                var list = [];
                var obj = {};
                let option = "";
                let notice_content = "";
                
                if (content && content.length > 0) {
                    for (let item of content) {
                        obj["notice_id"] = item.notice_id
                        obj["badge"] = !item.no_read;
                        notice_content = item.notice_content;
                        obj["title"] = notice_content.type;
                        obj["process_id"] = notice_content.process_id;
                        obj["subtitle"] = item.time;
                        let option = notice_content.status == COMPLETED ? "审批通过" : (notice_content.status == TERMINATED ? "审批拒绝" :(notice_content.status == CANCELED?"已撤销":"待审批"));
                        let color = option == "审批通过" ? "green" : (option == "审批拒绝" ? "red" : "");
                        obj["content"] = [
                            { "subcontent": "项目名称", "unitcontent": notice_content.proj_name },
                            { "subcontent": "项目编号", "unitcontent": notice_content.proj_num },
                            { "subcontent": "审批编号", "unitcontent": notice_content.process_id },
                            { "subcontent": option, "className": color }
                        ];
                        list.push(obj);
                        obj = {};
                        option = "";
                        notice_content = "";
                    }
                }
                const hasNoMore = list.length < props.pageSize;
                t.data.listData = list;
                if (props.proj_name || props.proj_num)
                    t.updateComponent()
                if (success)
                    success(hasNoMore, list);
                //设置角标
                // cb && cb(num);
                cb && cb(WDCount);
                
            })
            .catch((error) => {

            });
    },
    onUpdataNotice: function (notice_id) {
        DB.Notice.update_notice.config.query = { notice_id }
        DB.Notice.update_notice({
            no_read: true
        })
            .then((content) => {

            })
            .catch((error) => {

            });
    },
    onDeleteNotice: function (notice_id, cb) {

        DB.Notice.delete_notice({ notice_id })
            .then((content) => {
                if (!content) {
                    cb && cb();
                }
            })
            .catch((error) => {

            });
    },
    updateComponent: function () {
        this.trigger(this.data);
    }
});
